/*
    Dominate 3D - A cube-based game of life

    Copyright (C) 2007, Craig H. Miller (kidmosey@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdlib.h>

#include "vector.h"
//#include "float4.h"
//#include "vertex.h"

//-------------------------------------------------------------------
//	UTILITY FUNCTIONS
//-------------------------------------------------------------------
static void atof_array(
					   const char * const strIn,
					   float arr[],
					   int count,
					   char delim[] = " \t,"
					   )
{
	char *str = strdup(strIn);

	char *tok = strtok(str, delim);
	for (int i = 0; tok && i < count; i++)
	{
		arr[i] = (float )atof(tok);
		tok = strtok(NULL, delim);
	}

	delete str;
}

//-------------------------------------------------------------------
//	CLASS: CVECTOR3D
//-------------------------------------------------------------------

CVector3D::CVector3D(float _x, float _y, float _z)
: x(_x), y(_y), z(_z)
{
}

CVector3D::CVector3D(const char *strIn)
{
	atof_array(strIn, (float *)this, 3);
}

CVector3D::~CVector3D()
{
}

CVector3D CVector3D::operator+ (const CVector3D &rhs) const
{
	return CVector3D(
		x + rhs.x,
		y + rhs.y,
		z + rhs.z
	);
}

CVector3D &CVector3D::operator+= (const CVector3D &rhs)
{
	*this = *this + rhs;
	return *this;
}

CVector3D CVector3D::operator- (const CVector3D &rhs) const
{
	return CVector3D(
		x - rhs.x,
		y - rhs.y,
		z - rhs.z
	);
}

CVector3D CVector3D::operator- (void) const
{
	return CVector3D(-x, -y, -z);
}

CVector3D CVector3D::operator* (float rhs) const
{
	return CVector3D(x * rhs, y * rhs, z * rhs);
}

CVector3D &CVector3D::operator*= (float rhs)
{
	x *= rhs;
	y *= rhs;
	z *= rhs;

	return *this;
}

bool CVector3D::operator== (const CVector3D &rhs) const
{
	if (x - rhs.x >  0.001f ||
		x - rhs.x < -0.001f)
		return false;

	if (y - rhs.y >  0.001f ||
		y - rhs.y < -0.001f)
		return false;

	if (z - rhs.z >  0.001f ||
		z - rhs.z < -0.001f)
		return false;

	return true;
}
/*
//-------------------------------------------------------------------
//	CLASS: CFLOAT4
//-------------------------------------------------------------------

CFloat4::CFloat4(float _x, float _y, float _z, float _w)
: x(_x), y(_y), z(_z), w(_w)
{
}

CFloat4::CFloat4(const char *strIn)
{
	atof_array(strIn, (float *)this, 4);
}

CFloat4::~CFloat4()
{
}

CFloat4 CFloat4::operator= (CVector3D &rhs)
{
	x = rhs.x;
	y = rhs.y;
	z = rhs.z;

	return *this;
}

CFloat4 &CFloat4::operator+= (const CFloat4 &rhs)
{
	x += rhs.x;
	y += rhs.y;
	z += rhs.z;
	w += rhs.w;

	return *this;
}

CFloat4 &CFloat4::operator*= (const float rhs)
{
	x *= rhs;
	y *= rhs;
	z *= rhs;
	w *= rhs;

	return *this;
}

CFloat4 CFloat4::operator- (const CFloat4 &rhs) const
{
	return CFloat4(
		x - rhs.x,
		y - rhs.y,
		z - rhs.z,
		w - rhs.w
	);
}

CFloat4 &CFloat4::operator-= (const CFloat4 &rhs)
{
	x -= rhs.x;
	y -= rhs.y;
	z -= rhs.z;
    w -= rhs.w;

	return *this;
}

//-------------------------------------------------------------------
//	CLASS: CVERTEX
//-------------------------------------------------------------------

CVertex::CVertex()
: tx(0), ty(0)
{
}

CVertex::CVertex(const CVector3D &pos, float _tx, float _ty)
: position(pos), tx(_tx), ty(_ty)
{
}

const CVertex CVertex::operator+ (const CVertex &rhs) const
{
    return CVertex(position + rhs.position, 0, 0);
}

CVertex &CVertex::operator+= (const CVertex &rhs)
{
    position += rhs.position;
    return *this;
}

const CVertex CVertex::operator* (const float rhs) const
{
    return CVertex(position * rhs, 0, 0);
}

CVertex &CVertex::operator*= (const float rhs)
{
    position *= rhs;
    return *this;
}

CVertex::~CVertex()
{
}
*/
